import { ref, Ref } from "vue";

const utools = window.utools 

interface Result {
  getId: (id: string) => Promise<{} | null >;
  delId: (id: string) => void;
}

export function useId(): Result { 
  const getId = (id: string) => {
    console.log('延迟获取id 信息')
    let count: number = 0 
    let res: any = {
      _id: null
    }
    let promise = new Promise<object | null>(function(resolve, reject) { 
      let interval: any = null
      interval = setInterval(() => {
        if (count === 5) {
          clearInterval(interval)
          resolve(null)
        }
        res = utools.db.get(id)
        console.log('res', res);
        if (res && res._id) {
          count = 0
          resolve(res)
          clearInterval(interval)
        } else {
          count ++
          console.log('count', count);
        }
      },1000)
    })
    console.log('promise', promise);
    return promise;
  }

  // 清空获取id 信息
  const delId = (id: string): boolean => {
    console.log('删除获取不到数据得 id : ' + id)
    let res = utools.db.remove(id)
    return res && res.ok
  }

  return {
    getId,
    delId
  }
}